/**
 * 非商机拜访记录审核
 */
define(function () {

    var self,
        userInfo,
        footData = {
            index: 0
        },
        g_bffl = []
    ;

    function setBffl(data) {
        data.forEach(function (v) {
            if (v.GWBN011.indexOf('{') != -1) {
                var _GWBN011 = JSON.parse(v.GWBN011);
                var GWBN011s = [];
                g_bffl.forEach(function (v1) {
                    if (v.GWBNORIG == v1.GWBFL001 && _GWBN011[v1.GWBFL002]) {
                        GWBN011s.push({text: v1.GWBFL004, msg: _GWBN011[v1.GWBFL002]});
                    }
                });

                v.bffl.pushArray(GWBN011s);
            }
        });
    }

    app.newModule({

        init: function () {
            self = this;
            self.mData = {};
            self.searchData = {
                keyword: ''
            };
            userInfo = app.userInfos.sysUser;
            self._super({
                _tag: 'p_bfjlsp',
                _sliderId: 'slider', // tabs
                _refreshAutoInit: true
            }, win);
        },
        getBffl: function (data) {//获取拜访记录内容分类
            app.ajax({
                xurl: "api/wx/bfjlsp/getGwbfList",
                cb: function (result) {
                    if (result.ok) {
                        g_bffl.pushArray(result.data.data, true);
                        setBffl(data);
                    }
                }
            });
        },
        _initRefresh: function () { // 实例化刷新组件
            if (self._refreshIds.length == 0) {
                self._refreshIds = ['scrollN', 'scrollY', 'scrollX'];
                self._refreshVueOpts = {
                    data: {
                        others: {
                            SID: userInfo.ID,
                            ORGID: userInfo.ORG_ID
                        }
                    }
                };
            }
            this._super(win);
        },
        _refreshCb: function (goods) {// 刷新组件回调函授
            var _super = this._super;
            app.ajax({
                xurl: "api/wx/bfjlsp/getBfjlspList",
                xdata: {
                    page:goods.page+1,
                    limit: goods.limit,
                    GWBNACTI: goods.type.replace('scroll', ''),
                    keyword: self.searchData.keyword
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.data;
                        if (data.length > 0) {
                            data.forEach(function (v) {
                                v.checked = false;
                                v.bffl = [];
                            });
                        }
                        _super({
                            obj: goods,
                            data: data,
                            nodata: self._refreshIndex == 0 ? '无未确认商机主题...' : (self._refreshIndex == 1 ? '无已确认商机主题...' : '无已作废商机主题...')
                        });
                        if (data.length > 0) {
                            if (g_bffl.length == 0) {
                                self.getBffl(data);
                            } else {
                                setBffl(data);
                            }
                        }
                    }
                }
            });
        },
        _slide: function (i) {
            footData.index = i;
        },
        _lazy: function () {
            this._super();
        },
        _search:function (data) {
            self.searchData.keyword = data.keyword.trim();
            self._refresh();
        },
        _ready: function () {
            Vue.component('bfjlsp-listitem', {
                template: app.getByid(doc, 'item-template'),
                props: {
                    model: Array,
                    other: Object
                },
                methods: {
                    statueN: function (item) {
                        if (item.GWBNACTI == 'N') {
                            return true;
                        } else {
                            return false;
                        }
                    },
                    statueY: function (item) {
                        if (item.GWBNACTI == 'Y') {
                            return true;
                        } else {
                            return false;
                        }
                    },
                    statueImg: function (item) {
                        if (item.GWBNACTI == 'Y') {
                            return 'comfirom';
                        } else if (item.GWBNACTI == 'N') {
                            return 'nonComfirom';
                        } else {
                            return '';
                        }
                    },
                    showDetail: function (item) {
                        app.openPage({
                            src: "hn-bfjlsp/bfjlsp-info.html",
                            args: {
                                item: item,
                                cb: function () {
                                    self._refresh();
                                }
                            }
                        });
                    },
                    confirm: function (item) {

                        var _data = {
                            CHARACTERISTICS: [{
                                GWBNACTI: 'Y',
                                GWBNID: item.GWBNID,
                                GWBNSEQ: item.GWBNSEQ
                            }]
                        };

                        app.ajax({
                            xurl: "api/wx/bfjlsp/updateByKey",
                            xdata: {
                                obj: app.jsonString(_data)
                            },
                            confirm: {
                                msg: '是否确认？'
                            },
                            cb: function (result) {
                                if (result.ok) {
                                    app.alert({
                                        msg: "确认成功",
                                        ok: function () {
                                            self._refresh();
                                        }
                                    });
                                } else {
                                    app.toast(result.data.message);
                                }
                            }
                        });
                    },
                    reconfirm: function (item) {
                        var _data = {
                            CHARACTERISTICS: [{
                                GWBNACTI: 'N',
                                GWBNID: item.GWBNID,
                                GWBNSEQ: item.GWBNSEQ
                            }]
                        };

                        app.ajax({
                            xurl: "api/wx/bfjlsp/updateByKey",
                            xdata: {
                                obj: app.jsonString(_data)
                            },
                            confirm: {
                                msg: '是否取消？'
                            },
                            cb: function (result) {
                                if (result.ok) {
                                    app.alert({
                                        msg: "取消成功",
                                        ok: function () {
                                            self._refresh();
                                        }
                                    });
                                } else {
                                    app.toast(result.data.message);
                                }
                            }
                        })
                    },
                    del: function (item) {
                        app.confirm({
                            msg: '是否作废？',
                            ok: function (e) {
                                var _data = {
                                    CHARACTERISTICS: [{
                                        GWBNACTI: 'X',
                                        GWBNID: item.GWBNID,
                                        GWBNSEQ: item.GWBNSEQ
                                    }]
                                };

                                app.ajax({
                                    xurl: "api/wx/bfjlsp/updateByKey",
                                    xdata: {
                                        obj: app.jsonString(_data)
                                    },
                                    cb: function (result) {
                                        if (result.ok) {
                                            app.alert({
                                                msg: "作废成功",
                                                ok: function () {
                                                    self._refresh();
                                                }
                                            });
                                        } else {
                                            app.toast(result.data.message);
                                        }
                                    }
                                })
                            }
                        });
                    }
                }
            });
            new Vue({
                el: doc.getElementById('footer'),
                data: footData,
                methods: {
                    checkall: function () {
                        var items = self._getRefreshObj().data;
                        for (var i = 0; i < items.length; i++) {
                            if (userInfo.ID != items[i].GWBN001) {
                                items[i].checked = true;
                            }
                        }
                    },
                    reconfirmlist: function () {
                        var items = self._getRefreshObj().data;
                        var list = items.filterKV({checked: true});
                        if (list.length == 0) {
                            app.toast("无选中项");
                            return;
                        }

                        var info = {
                            CHARACTERISTICS: []
                        };
                        for (var i = 0; i < list.length; i++) {
                            info.CHARACTERISTICS.push({
                                GWCUDOCNO: list[i].GWCUDOCNO
                            })
                        }

                        var _data = {
                            CHARACTERISTICS: []
                        };
                        for (var i = 0; i < list.length; i++) {
                            _data.CHARACTERISTICS.push({
                                GWBNACTI: 'N',
                                GWBNID: list[i].GWBNID,
                                GWBNSEQ: list[i].GWBNSEQ
                            })
                        }

                        app.ajax({
                            xurl: "api/wx/bfjlsp/updateByKey",
                            xdata: {
                                obj: app.jsonString(_data)
                            },
                            confirm: {
                                msg: '是否批量取消？'
                            },
                            cb: function (result) {
                                if (result.ok) {
                                    app.toast("批量取消成功");
                                    for (var i = 0, flag = true; i < items.length; flag ? i++ : i) {
                                        if (items[i].checked == true) {
                                            items.splice(i, 1);
                                            flag = false;
                                        } else {
                                            flag = true;
                                        }
                                    }
                                    self._refresh();
                                } else {
                                    app.toast(result.data.message);
                                }

                            }
                        })

                    },
                    confirmlist: function () {
                        var items = self._getRefreshObj().data;
                        var list = items.filterKV({checked: true});
                        if (list.length == 0) {
                            app.toast("请选择需要确认的商机主题");
                            return;
                        }

                        var _data = {
                            CHARACTERISTICS: []
                        }
                        for (var i = 0; i < list.length; i++) {
                            _data.CHARACTERISTICS.push({
                                GWBNACTI: 'Y',
                                GWBNID: list[i].GWBNID,
                                GWBNSEQ: list[i].GWBNSEQ
                            })
                        }
                        app.ajax({
                            xurl: "api/wx/bfjlsp/updateByKey",
                            xdata: {
                                obj: app.jsonString(_data)
                            },
                            confirm: {
                                msg: '是否批量确认？'
                            },
                            cb: function (result) {
                                if (result.ok) {
                                    app.toast("批量确认成功");
                                    for (var i = 0, flag = true; i < items.length; flag ? i++ : i) {
                                        if (items[i].checked == true) {
                                            items.splice(i, 1);
                                            flag = false;
                                        } else {
                                            flag = true;
                                        }
                                    }
                                    self._refresh();
                                } else {
                                    app.toast(result.data.message);
                                }
                            }
                        })
                    },
                    deletelist: function () {
                        var items = [];
                        var list = self._getRefreshObj().data;
                        for (var i = 0; i < list.length; i++) {
                            if (list[i].checked == true) {
                                items.push(list[i])
                            }
                        }
                        if (items.length == 0) {
                            app.toast('无选中项');
                            return;
                        }
                        app.confirm({
                            msg: '是否作废？',
                            ok: function (e) {
                                app.ajax({
                                    xurl: "api/wx/gwat310/deletelist",
                                    xdata: {
                                        items: JSON.stringify(items)
                                    },
                                    cb: function (result) {
                                        for (var i = 0; i < result.data.data.length; i++) {
                                            for (var j = 0; j < items.length; j++) {
                                                if (items[j].GWCSDOCNO == result.data.data[i].GWCSDOCNO) {
                                                    list.remove(items[j]);
                                                }
                                            }
                                        }
                                        if (result.ok) {
                                            app.toast('批量作废成功');
                                        } else {
                                            app.toast(result.data.message);
                                        }
                                    }
                                });
                            }
                        });
                    }
                }
            });
        }
    });

});